package com.ac.app.web.action.msg;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ac.app.dm.PickInvest;
import com.ac.app.dm.PickOrg;
import com.ac.app.dm.PickRecord;
import com.ac.app.web.form.MsgForm;
import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class EditMsgAction extends BaseAction {
    protected ActionForward doExecute(ActionMapping mapping,
                                      BaseForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
            throws Exception {
    	MsgForm myForm=(MsgForm)form;
    	String orgId=request.getParameter("orgId");
    	
    	String content = null;
    	if("1".equals(myForm.getFlag())){
    		content =(String) this.getJdbcTemplate().queryForObject("select tmp_content from AC_TEMPLATE where TMP_TYPE='1'", String.class);
    	}else{
    		content =(String) this.getJdbcTemplate().queryForObject("select tmp_content from AC_TEMPLATE where TMP_TYPE='2'", String.class);
    	}
    		
    	if(orgId!=null){
    		PickOrg dm=(PickOrg)this.getBaseManager().getObjectById(PickOrg.class, orgId);
    		if("1".equals(myForm.getFlag())){
    			myForm.setAdds(dm.getEmail());
    		}else{
    			myForm.setAdds(dm.getMobile());    			
    		}
    	}else{
        	String investId=request.getParameter("investId");
    		PickInvest dm=(PickInvest)this.getBaseManager().getObjectById(PickInvest.class, investId);
    		if("1".equals(myForm.getFlag())){
    			myForm.setAdds(dm.getEmail());
    		}else{
    			myForm.setAdds(dm.getMobile());    			
    		}
    	}
		String[] recordIds = request.getParameterValues("recordId");
		if(recordIds!=null){			
			String strWhere ="";
			for(int i=0;i<recordIds.length;i++){
				strWhere += " or id='"+recordIds[i]+"'";
			}
			String con =""; 
			if(strWhere.length()>10){
				strWhere = strWhere.substring(3);
				List dmList = this.getBaseManager().getObjects(PickRecord.class, strWhere);
				for(int i=0;i<dmList.size();i++){
					PickRecord dm = (PickRecord) dmList.get(i);
					con += dm.getPrjName()+" ";
				}
			}
			myForm.setContent(content.replace("${content}", con));
		}else{
			myForm.setContent(content.replace("${content}", ""));
		}
		
        return forward(mapping);
    }
}